package xjh.him.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xjh.him.R;

import java.time.LocalDateTime;
import java.util.List;

import xjh.him.bean.NewFriendBean;
import xjh.him.common.SharedData;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.FriendDto;
import xjh.him.util.GlideUtil;
import xjh.him.util.ToastUtil;

public class NewFriendAdapter extends RecyclerView.Adapter<NewFriendAdapter.ViewHolder> {
    private List<NewFriendBean> beans;

    public NewFriendAdapter(List<NewFriendBean> beans) {
        this.beans = beans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.new_friend_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bindView(beans.get(position));
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {
        private View curView;
        private ImageView avatarImage;
        private TextView nameText, applyInfoText, handleText;

        public ViewHolder(@NonNull View v) {
            super(v);
            curView = v;
            avatarImage = v.findViewById(R.id.new_friend_avatar);
            nameText = v.findViewById(R.id.new_friend_name);
            applyInfoText = v.findViewById(R.id.new_friend_apply_info);
            handleText = v.findViewById(R.id.new_friend_handle);
        }

        public void bindView(NewFriendBean bean) {
            GlideUtil.loadImage(curView.getContext(), bean.getUser().getAvatarUrl(),
                    avatarImage, GlideUtil.defaultRoundedAvatarOpt());
            nameText.setText(bean.getUser().getNickname());
            applyInfoText.setText(bean.getInfo().getApplyInfo());

            if (bean.getInfo().getState() == SharedData.STATE_AGREE) {
                handleText.setText("已添加");
                handleText.setBackgroundColor(0);
            }

            handleText.setOnClickListener((v) -> {
                if (bean.getInfo().getState() == SharedData.STATE_APPLY && !hasExpired(bean.getInfo().getCreateTime())) {
                    FriendDto.HandleApplyReq req = new FriendDto.HandleApplyReq();
                    req.setState(SharedData.STATE_AGREE);
                    req.setApplyId(bean.getInfo().getId());
                    SharedData.client.sendMsg(Packet.create(FriendDto.HandleApplyReq.class, req));
                    Object obj = SharedData.future.get();
                    if (obj instanceof FriendDto.HandleApplyResp) {
                        handleText.setText("已添加");
                        handleText.setBackgroundColor(0);
                        if (SharedData.friendAdapter == null) {
                            SharedData.friendBeans.add(bean.getUser());
                        }else {
                            SharedData.friendAdapter.addLast(bean.getUser());
                        }
                        int count = Integer.parseInt(SharedData.countBubbleText.getText().toString()) - 1;
                        SharedData.countBubbleText.setText(String.valueOf(count));
                        if (count == 0)
                            SharedData.countBubbleText.setVisibility(View.INVISIBLE);
                        ToastUtil.show(curView.getContext(), "添加成功");
                    }
                }
            });
        }

        // 好友申请只有三天有效期
        public boolean hasExpired(LocalDateTime createTime) {
            int time = LocalDateTime.now().getSecond() - createTime.getSecond();
            return time >= 3600 * 24 * 3;
        }
    }
}
